package common
{
	import flash.display.MovieClip;
	import flash.system.ApplicationDomain;

	public class ObjectUtil
	{
		

		public static function createMovieClip(source:MovieClip, className:String):MovieClip
		{
			var movieclip:MovieClip = ObjectUtil.createObject(source, className) as MovieClip;
			
			if (movieclip == null)
				throw new Error("Cannot create MovieClip " + className + " from source MovieClip " + source.name);
			
			return movieclip;
		}
		
		
		
		
		/**
		 * Creates a new instance of a class from a definition in a specified MovieClip
		 * 
		 * @param	source	The source MovieClip which contains the class definition
		 * @param	className	The name of the class to instance
		 * @throws	Error	The class definition must exist in the specified MovieClip
		 */
		public static function createObject(source:MovieClip, className:String):Object
		{
			var appDomain:ApplicationDomain = source.loaderInfo.applicationDomain;
			
			if (!appDomain.hasDefinition(className))
				throw new Error("Class " + className + " does not exist in source MovieClip " + source.name);
			
			var classReference:Class =  appDomain.getDefinition(className);
			
			var classInstance:Object = new ClassReference();
			
			return classInstance;
		}
		
		
	}
}